package com.myteam.fugaocrm.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;

import com.myteam.fugaocrm.modle.FgPartnerCompany;
import com.myteam.fugaocrm.service.PartnerCompanyServiceImpl;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 供应商公司Mapper
 * @author BZX
 *
 */
@Controller
public class PartnerCompanyController {
	@Resource protected PartnerCompanyServiceImpl partnerCompanyServiceImpl;
	
	@RequestMapping("partner/company/all")
	@ResponseBody
	public ModelAndView all(HttpServletRequest request, HttpServletResponse response) throws Exception {
		List<FgPartnerCompany> fgPartnerCompanyList = partnerCompanyServiceImpl.all();
		
		ModelAndView view = new ModelAndView();
		view.addObject("fgPartnerCompanyList", fgPartnerCompanyList);
		view.setViewName("arch_supplier_company.jsp");
		return view;
	}
	
	@RequestMapping("partner/company/delete")
	@ResponseBody
	public ModelAndView delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Integer partnerCompanyId = Integer.parseInt(request.getParameter("partnerCompanyId").toString());
		
		partnerCompanyServiceImpl.deleteOne(partnerCompanyId);
		
		ModelAndView view = new ModelAndView();
		view.setViewName("redirect:/partner/company/all");
		return view;
	}
	
	@RequestMapping("partner/company/insertorupdate")
	@ResponseBody
	public ModelAndView insertOrUpdate(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String partnerCompanyIdStr = request.getParameter("partnerCompanyId")+"";
		String partnerCompanyName = request.getParameter("partnerCompanyName")+"";
		
		//校验
		
		
		//插入或更新
		Date date = new Date();
		String staffName = request.getSession().getAttribute("staffName")+"";
		Map<String, Object> params = new HashMap<String, Object>();
		
		if("".equals(partnerCompanyIdStr)) {
			//insert
			params.put("partnerCompanyName", partnerCompanyName);
			params.put("createdDate", date);
			params.put("createdUser", staffName);
			partnerCompanyServiceImpl.insert(params);
		}else {
			//update
			Integer partnerCompanyId = Integer.parseInt(partnerCompanyIdStr);
			params.put("partnerCompanyId", partnerCompanyId);
			params.put("partnerCompanyName", partnerCompanyName);
			params.put("modifiedDate", date);
			params.put("modifiedUser", staffName);
			partnerCompanyServiceImpl.update(params);
		}
		
		ModelAndView view = new ModelAndView();
		view.setViewName("redirect:/partner/company/all");
		return view;
	}
}
